//
//  ProfileViewMyAvatarCell.swift
//  iModern
//
//  Created by Ralf on 5/14/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class ProfileViewMyAvatarCell: TableViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
    var contact: NSDictionary!

    let __background_image_view_tag = 10; let __avatar_image_view_tag = 11;

    let __alias_label_tag = 20; let __about_me_label_tag = 21; let __signature_label_tag = 22;

    var imageType = 0

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initBackgroundImageView()
        initAvatarImageView()
        initAliasLabel()
        //        initAboutMeLabel()
        initSignatureLabel()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setData(data: NSDictionary)
    {
        self.contact = data
        let ivAvatar = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
        let imageURL = data["imageURL"] as? String

        if (imageURL != nil)
        {
            ivAvatar.load(appDelegate.parameterManager.server + "/" + imageURL!, placeholder: nil)
        }

        let backgroundURL = data["backgroundURL"] as? String

        if (backgroundURL != nil)
        {
            let ivBackground = self.viewWithTag(__background_image_view_tag) as! UIImageView
            ivBackground.load(appDelegate.parameterManager.server + "/" + backgroundURL!, placeholder: nil)
        }

        let lAlias = self.viewWithTag(__alias_label_tag) as! UILabel
        lAlias.text = contact["alias"] as? String

        let signature = data["signature"] as? String
        if signature != nil
        {
            if (signature!).utf8.count > 0
            {
                let lSignature = self.viewWithTag(__signature_label_tag) as! UILabel
                lSignature.text = signature
            }
        }
        else if signature == nil
        {
            let lSignature = self.viewWithTag(__signature_label_tag) as! UILabel
            lSignature.text = "签名 (默认签名:我就是我,是颜色不一样的烟火!爱手工 爱生活!)"
        }
    }

    func initBackgroundImageView()
    {
        var imageView = self.viewWithTag(__background_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 0, y: 0, width: screen.size.width, height: 200)
            imageView = UIImageView(frame: frame)
            imageView!.tag = __background_image_view_tag
            imageView!.contentMode = .ScaleToFill
            imageView!.image = UIImage(named: "Avatar-Backgound.png")

            self.addSubview(imageView!)
        }

        let tgUploadAvatar = UITapGestureRecognizer(target: self, action: "uploadBackground:")
        imageView!.addGestureRecognizer(tgUploadAvatar)
        imageView!.userInteractionEnabled = true
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let x     = (screen.size.width - 100) / 2
            let frame = CGRect(x: x, y: 30, width: 100, height: 100)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __avatar_image_view_tag

            imageView!.layer.cornerRadius = 50
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1

            self.addSubview(imageView!)
        }

        let tgUploadAvatar = UITapGestureRecognizer(target: self, action: "uploadAvatar:")
        imageView!.addGestureRecognizer(tgUploadAvatar)
        imageView!.userInteractionEnabled = true
    }

    func initAliasLabel()
    {
        var label = self.viewWithTag(__alias_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 0, y: 130, width: screen.width, height: 40)
            label = UILabel(frame: frame)
            label!.textAlignment = .Center
            label!.tag = __alias_label_tag
            label!.font = UIFont.systemFontOfSize(25)
            label!.textColor = UIColor.whiteColor()
            label!.shadowColor = UIColor.darkGrayColor()
            label!.shadowOffset = CGSizeMake(1, 1)
            self.addSubview(label!)
        }

        let tgUpdateProfile = UITapGestureRecognizer(target: self, action: "updateProfile:")
        label!.userInteractionEnabled = true
        label!.addGestureRecognizer(tgUpdateProfile)
    }

    //    func initAboutMeLabel()
    //    {
    //        var label = self.viewWithTag(__about_me_label_tag) as? UILabel
    //        if (label == nil)
    //        {
    //            let frame = CGRect(x: 0, y: 170, width: screen.width, height: 20)
    //            label = UILabel(frame: frame)
    //            label!.textAlignment = .Center
    //            label!.tag = __about_me_label_tag
    //            label!.font = UIFont.systemFontOfSize(12)
    //            label!.textColor = UIColor.whiteColor()
    //            label!.shadowColor = UIColor.darkGrayColor()
    //            label!.shadowOffset = CGSizeMake(1, 1)
    //            label!.text = "看看什么代表了你"
    //            self.addSubview(label!)
    //        }
    //
    //        let tgUpdateProfile = UITapGestureRecognizer(target: self, action: "updateProfile:")
    //        label!.userInteractionEnabled = true
    //        label!.addGestureRecognizer(tgUpdateProfile)
    //    }

    func initSignatureLabel()
    {
        var label = self.viewWithTag(__signature_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 0, y: 170, width: screen.width, height: 20)
            label = UILabel(frame: frame)
            label!.textAlignment = .Center
            label!.tag = __signature_label_tag
            label!.font = UIFont.systemFontOfSize(11)
            label!.textColor = UIColor.whiteColor()
            label!.shadowColor = UIColor.darkGrayColor()
            label!.shadowOffset = CGSizeMake(1, 1)
            label!.text = "个性签名"
            self.addSubview(label!)
        }

        let tgUpdateProfile = UITapGestureRecognizer(target: self, action: "updateProfile:")
        label!.userInteractionEnabled = true
        label!.addGestureRecognizer(tgUpdateProfile)
    }

    func uploadAvatar(sender: UITapGestureRecognizer)
    {
        //let ivAvatar = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
        imageType = 0
        //ivAvatar.shake(.Vertical)
        let imagePicker = UIImagePickerController()

        imagePicker.sourceType = .PhotoLibrary; imagePicker.delegate = self; imagePicker.allowsEditing = true;

        appDelegate.navigationController.presentViewController(imagePicker, animated: true, completion: nil)
    }

    func uploadBackground(sender: UITapGestureRecognizer)
    {
        imageType = 1
        //let ivBackground = self.viewWithTag(__background_image_view_tag) as! UIImageView
        //ivBackground.shake(.Vertical)
        let imagePicker = UIImagePickerController()
        imagePicker.videoQuality = .Type640x480
        imagePicker.sourceType = .PhotoLibrary; imagePicker.delegate = self; //        imagePicker.allowsEditing = true;

        appDelegate.navigationController.presentViewController(imagePicker, animated: true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject:AnyObject]!)
    {
        if imageType == 0
        {
            let ivAvatar = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
            appDelegate.contactManager.uploadAvatar(image)
            //let size = CGSize(width: 640, height: 640)
            ivAvatar.image = image
            appDelegate.navigationController.dismissViewControllerAnimated(true, completion: nil)
        }
        else
        {
            let ivBackground = self.viewWithTag(__background_image_view_tag) as! UIImageView
            appDelegate.contactManager.uploadBackground(image)
            //let size = CGSize(width: 640, height: 640)
            ivBackground.image = image
            appDelegate.navigationController.dismissViewControllerAnimated(true, completion: nil)
        }
    }

    func updateProfile(sender: UITapGestureRecognizer)
    {
        //        let controller = PersonalInformationEditViewController()
        let controller = PersonalInformationFormViewController()
        self.pushViewController(controller)
    }
}
